<template>
  <div>
    <el-form :inline="true" :model="search" class="demo-form-inline">
       <el-row class="table-head-box">
         <el-col :span="4" class="flex flex-start">
           <el-button type="primary" icon="el-icon-plus"    plain @click="addHandler()">添加百科</el-button>
        </el-col>
        <el-col :span="20" class="flex flex-end input-box">
          <el-form-item  label="标题:">
            <el-input v-model="search.lkParam.title" clearable placeholder="标题"></el-input>   
     </el-form-item>
     <el-form-item  label="宠物分类:">
          <el-cascader v-model="search.eqParam.pet_type_id" :options="petTypeList"
           :props="{ value: 'typeId',label:'typeName',emitPath:false }"
           clearable></el-cascader>      
     </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="searchHandler">查询</el-button>
        

     </el-form-item>
        </el-col>

      </el-row>
      

    </el-form>
    <el-table :data="petEncyclopediaList" border style="width: 100%" tooltip-effect="dark" size="mini">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column  prop="title" label="标题" >
      </el-table-column>
      <el-table-column  prop="picture" label="图片" >
         <template slot-scope="scope">
            <span><img :src="scope.row.picture" width="50px" height="50px"></span>
          </template>
      </el-table-column>
      <el-table-column  prop="visitNumb" label="访问量" >
      </el-table-column>
      <el-table-column prop="petTypeId" label="宠物类型编号" >
      </el-table-column>
      <el-table-column prop="petTypeName" label="宠物类型" >
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间">
      </el-table-column>
      <!-- <el-table-column prop="have" label="状态" >
        <template slot-scope="scope">
          <el-switch
          v-model="scope.row.status"
          :inactive-value="0"
          :active-value="1"
          active-color="#13ce66"
          inactive-color="#ff4949"
          @change="updateStatusHandler(scope.row)"
          >
        </el-switch>
        </template>
      </el-table-column> -->

      <el-table-column fixed="right" label="操作" >
        <template slot-scope="scope">
          <el-button class="el-icon-edit" type="primary" size="mini" @click="updateHandler(scope.$index, scope.row)">编辑</el-button>
           <el-button
            type="success"
            class="el-icon-warning-outline"
            size="mini"
            @click="updateStatusHandler(scope.$index, scope.row)"
            >{{ scope.row.status == 1 ? "下架" : "上架" }}</el-button
          >
          <el-button class="el-icon-delete" type="danger" size="mini" @click="updateHandler(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row>
        <el-col :span="24" style="text-align:right;">
            <el-pagination background layout="total,prev, pager, next" 
            :page-count="search.size" 
            :total="search.total"
            :current-page="search.current"
             @current-change="sizeChangeHandler"> </el-pagination>
        </el-col>
    </el-row>
    <PetEncyclopedia :popDialog="updateDialog" :popData="petEncyclopedia" @close="close"></PetEncyclopedia>
    <PreviewPetEncyclopedia :previewDialog="previewDialog" :popData="petEncyclopedia"  @close="close"></PreviewPetEncyclopedia>
  </div>
</template>
<script>
import {petEncyclopediaPageListUrl,petEncyclopediaSaveUrl,petTypeListUrl} from '@/utils/common'
import PetEncyclopedia from './pet-encyclopedia-add'
import PreviewPetEncyclopedia from './preview-encyclopedia'

export default {
  components: {
    PetEncyclopedia,
    PreviewPetEncyclopedia
  },
  data() {
    return {
      petEncyclopediaList: [],
      petTypeList:[],
      updateDialog: false,
      previewDialog:false,
      petEncyclopedia: null,

      search: {
        current:1,
        size:10,
        total: 0,
        lkParam: {userName:""},
        eqParam:{},
      }
    };
  },
  
  mounted() {
    this.loadPageList();
    this.loadPetType();
  },
  methods: {
    addHandler: function () {
      this.petEncyclopedia = {};
      this.updateDialog = true;
    },
    updateHandler: function (index, row) {
      
      this.petEncyclopedia = row;
      this.updateDialog = true;
    },
    closeHandler: function () {
      this.updateDialog = false;
      this.loadPageList();
    },
     closePreviewHandler: function () {
      this.previewDialog = false;
    },
    previewHandler:function(index,row){
      this.petEncyclopedia=row;
      this.previewDialog=true;
    },
    loadPetType(){
      this.axios.post(petTypeListUrl,{}).then(res=>{
        this.petTypeList = res.data.data;
      })
    },
    /**
     * @description: 关闭弹框
     * @param {*}
     * @return {*}
     */   
    close(){
      this.updateDialog = false
       this.previewDialog = false;
    },
    updateStatusHandler(row){
      this.axios
        .post(petEncyclopediaSaveUrl,row)
        .then(res => {
          if (res.data.status == "SUCCESS") {
            this.loading = false;
            this.$emit("close");
            this.$message.success(res.data.result);
          } else {
            this.loading = false;
            this.$message.error(res.data.result);
          }
        })
        .catch(response => {
          this.loading = false;
          console.log("response - " + response);
          this.$message.error("数据保存异常失败");
        });
    },
    /**
     * 删除用户
     */
    // deleteUserHandler: function (index, row) {
    //   this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", { confirmButtonText: "确定", cancelButtonText: "取消", type: "warning" })
    //     .then(() => {
    //       this.axios
    //         .post("/fc/su/del/" + row.userId)
    //         .then(response => {
    //             console.log(response)
    //           this.$message({ type: "success", message: "删除成功!"  });
    //           this.loadPetPublish();
    //         })
    //         .catch(response => {
    //             console.log(response)
    //           this.$message({ type: "warn", message: "删除失败!"  });
    //         });
    //     })
    //     .catch(() => {
    //       this.$message({ type: "info", message: "已取消删除" });
    //     });
    // },
    loadPageList() {
      this.axios
        .post(petEncyclopediaPageListUrl, this.search)
        .then(response => {
          var data = response.data.data;
          this.petEncyclopediaList = data.records;
          this.search.total =data.total;
        })
        .catch(response => {
          console.log("response -   " + response);
        });
    },
    sizeChangeHandler: function (value) {
      this.search.current = value;
      this.loadPageList();
    },
    searchHandler: function () {
      this.search.current = 1;
      console.log(this.search.eqParam)
      this.loadPageList();
    }
  }
};
</script>
<style scoped>
.grid-content {
  text-align: left;
  height: 45px;
}
</style>
